Skip to content

Eliminate redundant type assertions #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2020
Merged

Eliminate redundant type assertions #1156

merged 3 commits into from
Mar 11, 2020

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Mar 11, 2020

This PR refactors the compiler sources with the intent to eliminate any redundant type assertions that might involve a redundant dynamic check at runtime when compiling to WebAssembly.

Typical patterns are

if (element.kind == ElementKind.FUNCTION) {
  doSomething(<Function>element);
  doSomethingElse(<Function>element);
}

which are nops in TS but upcasts in AS, where refactoring these to

if (element.kind == ElementKind.FUNCTION) {
  let functionInstance = <Function>element;
  doSomething(functionInstance);
  doSomethingElse(functionInstance);
}

does just one dynamic check instead of two at the expense of an additional local that Binaryen will most likely collapse again.

Also makes relevant code a little more verbose as a side-effect, which some might find helpful to keep track of things when working with the sources.

@dcodeIO dcodeIO requested a review from MaxGraey March 11, 2020 20:02
@@ -93,8 +93,8 @@ export abstract class ExportsWalker {
visitElement(name: string, element: Element): void {
if (element.is(CommonFlags.PRIVATE) && !this.includePrivate) return;
var seen = this.seen;
if (seen.has(element) && !element.is(CommonFlags.INSTANCE)) {
this.visitAlias(name, element, <string>seen.get(element));
if (!element.is(CommonFlags.INSTANCE) && seen.has(element)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Member

@MaxGraey MaxGraey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dcodeIO dcodeIO merged commit 03aff6e into master Mar 11, 2020
@dcodeIO dcodeIO deleted the typeasserts branch March 15, 2020 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants